package com.qianfeng.yime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.*;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.yime.model.User;
import com.qianfeng.yime.util.CommonUtils;
import com.qianfeng.yime.util.CurrentSingleton;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by IDEA
 * User : SL
 * on  2015/3/10.
 * 12:08
 */

/**
 * 用户登录
 */
public class UserLoginActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    private EditText name;
    private EditText psw;
    private CheckBox checkBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);

        setTitle(R.string.login);

        name = (EditText) findViewById(R.id.edit_user_login_name);
        psw = (EditText) findViewById(R.id.edit_user_login_psw);
        checkBox = (CheckBox) findViewById(R.id.check_user_login_checked);

        Button btnLogin = (Button) findViewById(R.id.btn_user_login);
        TextView register = (TextView) findViewById(R.id.text_user_register);
        TextView forget = (TextView) findViewById(R.id.text_user_forget_psw);

        btnLogin.setOnClickListener(this);
        register.setOnClickListener(this);
        forget.setOnClickListener(this);

        checkBox.setOnCheckedChangeListener(this);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int id = v.getId();

        String nameStr = name.getText().toString();
        String pswStr = psw.getText().toString();

        pswStr = CommonUtils.urlMDToString(pswStr);
        System.out.println("pswStr = " + pswStr);

        Intent intent = null;

        switch (id){
            case R.id.btn_user_login:

                HttpUtils httpUtils = new HttpUtils();
                String url = Constants.USER_LOGIN_URL + "?loginName=" + nameStr + "&loginPassword=" + pswStr;
                System.out.println("url = " + url);
                httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
//                        System.out.println("result = " + result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            int code = jsonObject.getInt("code");
                            if (code == 200){
                                JSONObject data = jsonObject.getJSONObject("data");
                                JSONObject stu = data.getJSONObject("stu");
                                User user = new User();
                                user.parseJson(stu);

                                CurrentSingleton singleton = CurrentSingleton.getInstance();
                                singleton.setUser(user);
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        System.out.println("msg = " + msg);
                    }
                });

                break;
            case R.id.text_user_register:
                intent = new Intent(this, UserRegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.text_user_forget_psw:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        boolean checked = checkBox.isChecked();

        if (checked){
            psw.setInputType(InputType.TYPE_CLASS_TEXT);
        }else {
            psw.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }
}